"""
提供基础的函数  给所有的页面对象执行

"""
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import os,time

from common.get_log import BotLogger
from common.get_config import *

logger = BotLogger(os.path.basename(__file__), logname=1).getLogger()

class BasePage:

    def __init__(self,driver):
        self.driver = driver


    # def open(self):
    #     self.driver.get(self.url)
    #     self.driver.maximize_window()

    def quit(self):
        self.driver.quit()

    # def max(self):
    #     self.driver.maximize_window()
    # 定位元素
    def locter(self,loc, loc_desc=''):

        try:
            ele = self.driver.find_element(*loc)
        except Exception as e:
            logger.error("查找元素【{}】-失败".format(loc_desc))
            logger.exception(e)
            # 若定位报错，调用截图方法截图，传入截图描述
            self.save_pictuer(loc)
            raise e
        else:
            logger.info("查找元素【{}】-成功".format(loc_desc))
            return ele

    def input(self,loc, value, loc_desc=''):

        try:
            self.driver.find_element(*loc).clear()
            self.driver.find_element(*loc).send_keys(value)
            # self.locter(loc,loc_desc).clear()
            # self.locter(loc,loc_desc).send_keys(value)
        except Exception as e:
            logger.error("在input元素【{}】-输入值失败".format(loc_desc))
            logger.exception(e)
            # 若定位报错，调用截图方法截图，传入截图描述
            self.save_pictuer(loc_desc)
            raise e
        else:
            logger.info("在input元素【{}】-输入值成功".format(loc_desc))

    def click(self, loc, loc_desc= ''):
        try:
            # self.locter(loc,loc_desc).click()
            self.driver.find_element(*loc).click()
        except Exception as e:
            logger.error("点击元素【{}】-失败".format(loc_desc))
            logger.exception(e)
            # 若定位报错，调用截图方法截图，传入截图描述
            self.save_pictuer(loc)
            raise e
        else:
            logger.info("点击元素【{}】-成功".format(loc_desc))

    # 截图
    def save_pictuer(self, doc=''):
        # images_dir = r_config(conf_dir, 'report', 'img_path')
        images_dir = ReadConfig().get_Report('img_path').replace('base_dir',BASE_DIR)
        # logger.info(images_dir)
        # print(images_dir)
        filePath = images_dir + '{0}_{1}.png'.format(doc, time.strftime('%Y-%m-%d_%H_%M_%S', time.localtime()))
        # logger.info(filePath)
        # print(filePath)
        try:
            self.driver.save_screenshot(filePath)
            logger.info('{0}截图成功，图片路径为: {0}'.format(doc, filePath))

        except:
            logger.info('{0}截图 失败'.format(doc))

    # 等待元素可见
    def wait_visibility_ele(self, loc, loc_desc='', timeout=20, poll_time=0.5):
        """
        等待元素可见
        :param loc: 页面元素
        :param loc_desc: 描述
        :param timeout: 最大等待时间
        :param poll_time: 等待的轮询间隔时间
        :return: 定位到的元素element
        """
        try:
            ele = WebDriverWait(self.driver, timeout, poll_time).until(
                EC.visibility_of_element_located(loc)
            )
        except Exception as e:
            logger.error("等待元素【{}】可见超时".format(loc_desc))
            logger.exception(e)
            # 若定位报错，调用截图方法截图，传入截图描述
            self.save_pictuer(loc_desc)
            raise e
        else:
            logger.info("等待元素【{}】可见成功".format(loc_desc))
            return ele

    # 获取元素文本
    def get_element_text(self, loc, loc_desc=''):
        """
        获取元素文本
        :param loc:
        :param loc_desc:
        :return: 元素的文本text
        """
        try:

            text = self.driver.find_element(*loc).text
        except Exception as e:
            logger.error("获取元素【{}】文本-失败".format(loc_desc))
            logger.exception(e)
            # 若定位报错，调用截图方法截图，传入截图描述
            self.save_pictuer(loc_desc)
            raise e
        else:
            logger.info("获取元素【{}】文本-成功".format(loc_desc))
            return text